﻿using SCG.General;
using System;
using System.Net;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //JumpGateShips
    public class JumpGateShip : StarShip
    {
        //constructor
        public JumpGateShip(SVGame game, Player owner, int engines, StarSystem origin)
            : base(game, owner, engines, 0, origin)
        {
        }

        //Ship name
        public override string Name
        {
            get
            {
                return "JumpGateShip";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "JumpGateShips can build Wormholes that lead from their StarSystem of origin to the point in space the JumpGateShip exists at the time the Wormhole was created. Only 1 Wormhole can be created at one StarSystem.  JumpGateShips are destroyed when creating their Gate.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.JumpGateShip;
            }
        }

        //they can create a gate if one wasn't created at origin starsystem
        public override bool CanExecuteOrder(OrderType ot)
        {
            switch (ot)
            {
                case OrderType.CreateGate:
                    return OriginStarSystem != null && !OriginStarSystem.JumpGateCreated;
                default:
                    return base.CanExecuteOrder(ot);
            }
        }

        //create the jumpgate
        internal void CreateJumpGate()
        {
            //abort if a gate was created
            if (OriginStarSystem == null || OriginStarSystem.JumpGateCreated)
                return;

            //Put the gate at the first empty space next to the origin star system
            SCGPoint pt = Game.ClosestEmptyPoint(OriginStarSystem.Location);

            //create the wormhole
            Wormhole jumpGate = new Wormhole(Game);
            jumpGate.DiscoveredBy[Owner] = true;
            jumpGate.IsJumpGate = true;
            jumpGate.SetLocation(pt.X, pt.Y);
            jumpGate.DestX = X;
            jumpGate.DestY = Y;

            //flag the StarSystem as having created a jumpgate
            OriginStarSystem.JumpGateCreated = true;

            //Submit state change        
            Game.SubmitStateChange(StateChange.CreateGate, this);

            //and destroy the ship
            Game.DestroyStarShip(this);

            //alert the host
            if (Game.Host != null)
                Game.Host.CreateGate(jumpGate);
        }
    }
}
